<?php
require ('../conf/config.inc');
require_once COMMON_LIB.'DVS/DVS.php';
require_once COMMON_LIB.'DVS/Dynamic.php';

set_time_limit (7200);

$dir = PROJECT_ROOT.'data/'.date('Ymd').'_reg';
//$dir = PROJECT_ROOT.'data/20110407_reg';

//DB_DataObject::DebugLevel(1);
$db_obj = DVS_Dynamic::createDbObj('countries');
//$db_obj->query("TRUNCATE countries_ha");
//$db_obj->query("TRUNCATE ha_id");

$start_time = time();

echo '<pre>';

getRegionsFile('', 0);

    function getRegionsFile($file, $parent_id=0)
    {
        global $dir, $N, $M;
        if (!$file) {
            $file = $dir.'/1.htm';
        }
        $str = file_get_contents($file);
        
        $match_arr = getLinks($str);

        //print_r($match_arr);
        $url_arr = $match_arr[0];
        $id_arr = $match_arr[1];
        //insertId($id_arr);
        //print_r($url_arr);
        //exit;
        foreach ($url_arr[2] as $k => $url) {
            $N++;
            insertCountry($url_arr[2][$k], $url_arr[4][$k], $url_arr[1][$k], $url_arr[5][$k], $parent_id);
            //echo $N.' '.$url_arr[2][$k].' '.$id.'<br>';
            //$file_name = $dir.'/'.$url_arr[4][$k].' rentals.txt';
            $file_name = $dir.'/'.$url_arr[2][$k].'.htm';
            if (file_exists($file_name)) {
                //echo $file_name.'<br>';
                 getRegionsFile($file_name, $url_arr[2][$k]);
            }
            flush();
        }
    }


    function getLinks($str)
    {
        $id_tpl = '/href="\/p(\d+)" rel="nofollow"><img src="([^"]+)"/';

        $regions_tpl = '/\/([^\/]+)\/\w?\/?(\d+)(.htm|\/fa\/find.squery|)" class="region-refinement">\n[\s]+([^\n]+) rentals[^(]+\(([^)]+)/';

        preg_match_all ($regions_tpl, $str, $regions_match);
        //preg_match_all ($id_tpl, $str, $id_match);

        return array($regions_match, $id_match);
    }

    /**
    1 alias
    2 id
    4 name
    5 counter
    */
    function insertCountry($id, $name, $alias, $counter, $parent_id=0)
    {
        $db_obj = DVS_Dynamic::createDbObj('countries');
        //DB_DataObject::DebugLevel(1);
        $db_obj->disable_sk = true;
        $db_obj->id = $id;
        $db_obj->parent_id = $parent_id;
        $db_obj->name = $name;
        $db_obj->ha_alias = $alias;
        $db_obj->ha_counter = str_replace(",", "", $counter);
        if ($db_obj->insert()) {
            return true;
        }
        return false;
    }



    function getCountry($name)
    {
        $db_obj = DVS_Dynamic::createDbObj('countries');
        //DB_DataObject::DebugLevel(1);
        $db_obj->get('name',  cleanName($name));
        if ($db_obj->id) {
            return $db_obj->id;
        }
        return false;
    }

    function cleanName($name)
    {
        return trim(str_replace('rentals', '', $name));
    }

    function insertId($id_arr)
    {
        global  $db_obj;
        foreach ($id_arr[1] as $k => $id) {
            $sql .= ($sql ? ',' : '').'('.$id.",'".$id_arr[2][$k]."')";
        }
        $sql = "INSERT into ha_id (id, img) VALUES ".$sql.";";
        //echo $sql;
        $db_obj->query($sql);
    }


echo $N.'<br>'.$M;
$end_time = time();
echo '<br>t: '.($end_time - $start_time).'c.<br>';

?>